export function formatNumber(number, count) {
    return parseFloat(number.toFixed(count))
}
// 数组去重
export function unique(arr) {
    var obj = {};
    arr = arr.reduce(function(item, next) {
        obj[next.id] ? '' : obj[next.id] = true && item.push(next);
        return item;
     }, []);
    return arr;
}

/**
 * 判断是否在数组中
 * @param {mixed} search 
 * @param {array} array 
 * @param {bool} flag 
 * @returns 
 */
export function in_array(search, array, flag = true) {
    for (const i in array) {
        if (flag === true && array[i] === search) {
            return true
        } else if (array[i] == search) {
            return true
        }
    }
    return false
}

/**
 * 获取题干最后一张图
 * @param {string} string
 * @returns 
 */
 export function stem_last_img(string, flag = 1) {
    // const imgRex = /(<img.*?src=\")(.*?)(\".*?>)/g;
    const imgRex = /<img.*?src="(.*?)"[^>]*>/ig;
    let matchResult = string.match(imgRex)
    matchResult = matchResult ? matchResult[matchResult.length - 1] : ''
    if (flag == 2) {
        let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
        let srcResult = matchResult.match(srcReg);
        return srcResult ? srcResult[1] : ''
    }

    return matchResult
}

/**
 * 设置题干最后一张图的宽高
 * @param {string} stem
 * @returns 
 */
 export function stem_last_img_style(stem) {
    let lastImg = stem_last_img(stem)
    return stem.replace(lastImg, lastImg.replace(/(\<img\s+((?!style).)+?)(\/?>)/gi, '$1 style="width: 400px;height: 100px;cursor: pointer;"$3'))
}